package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URLClassLoader;
import javax.imageio.ImageIO;

import Main.SpaceGame;
import World.TileType;

public class Door extends Construction
{
	int x,y;
	BufferedImage spriteOpen = null, spriteClosed = null;
	boolean closed = true;
	SpaceGame main;
	
	public Door(int x, int y,SpaceGame main) 
	{
		super(x, y,main);
		this.x = x;
		this.y = y;
		height = 1;width = 1;
		this.main = main;
		main.world.tiles[x][y].setType(TileType.Interior);
		main.world.tiles[x][y].setSolid(true);
		
		try {
			spriteOpen = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"DoorOpen.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			spriteClosed = ImageIO.read(URLClassLoader.getSystemResource("Resources/"+"DoorClosed.png"));
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		workingTile = main.world.tiles[x][y];
		setText();
	}

	@Override
	public void draw(Graphics g) 
	{
		if (main.world.tiles[x-1][y].isSolid() && main.world.tiles[x+1][y].isSolid())
		{
			if(isClosed())
				g.drawImage(spriteClosed, x*10, y*10, null);
			else
				g.drawImage(spriteOpen, x*10, y*10, null);
		}
		else
		{
			double rotationRequired = Math.toRadians(90);
			double locationX = spriteOpen.getWidth() / 2;
			double locationY = spriteOpen.getHeight() / 2;
			AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
			AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);

			if(isClosed())
				g.drawImage(op.filter(spriteClosed, null), x*10, y*10, null);
			else
				g.drawImage(op.filter(spriteOpen, null), x*10, y*10, null);
			
		}
		drawFlash(g, x, y);
	}
	
	public String getName()
	{
		return "Blast Door";
	}
	
	@Override
	public void setText() 
	{
		textColorIndex = 2;
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("Blast Door");
		
		colors.add(Color.cyan);
		colors.add(Color.cyan);
		text.add("");
		text.add("");
		
		colors.add(Color.green);
		text.add("Enter");
		colors.add(Color.cyan);
		text.add("- Open/Close door");
	}
	
	public void setClosed(boolean closed)
	{
		this.closed = closed;
		main.world.tiles[x][y].setSolid(closed);
	}
	
	public boolean isClosed()
	{
		return closed;
	}
	
	

	@Override
	public void keyPressed(KeyEvent e) 
	{
		if (e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			if(isClosed())
			{
				setClosed(false);
			}
			else
			{
				setClosed(true);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) 
	{
		
	}

	@Override
	public void keyTyped(KeyEvent e) 
	{
		
	}
}
